package org.web.demo.listener;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.Enumeration;

/**
 * @author yexi
 */
@WebListener
@Slf4j
public class CustomHttpSessionListener implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        HttpSession session = se.getSession();
        log.info("监听到{}对象创建", session);
        log.info("此SessionEvent的相关信息: " + se.toString());
        Enumeration<String> attributeNames = session.getAttributeNames();
        while (attributeNames.hasMoreElements()) {
            log.info(attributeNames.nextElement());
        }
        log.info(session.getServletContext().getContextPath());
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        log.info("监听到有一个Session对象销毁");
        HttpSession session = se.getSession();
        log.info("此SessionEvent的相关信息: " + se.toString());
    }

}